const mongoose = require("mongoose")
const AdminUser = require("./../models/AdminUser");
const adminMessage = require("./../models/AdminMessage");
const assert = require("http-assert");
const AdminMessage = require("./../models/AdminMessage");

//创建用户
exports.createAdmin = async (req, res) => {

    // res.send("create");
    const userName = await AdminUser.findOne({name: req.body.name});
    assert(!userName, 422, "已有此用户名");
    // console.log(req.body);
    const model = await AdminUser.create(req.body);
    console.log(model);
    const message = await AdminMessage.create({
        name: req.body.name,
        userId: model._id,
        staying: [model.date, ""],
        authoritylever: 1
    });
    console.log(message);
    res.send({
        code: 200,
        user: model,
        message: message,
        authoritylever: 1
    })
}

//用户列表
exports.adminList = async (req, res) => {
    const model = await AdminUser.find();
    // console.log(model);
    res.send(model)
}

//删除用户
exports.deleteAdmin = async (req, res) => {
    const user = await AdminMessage.findOne({userId: req._id});
    console.log("*****", user)
    const authoritylever = user.authoritylever;
    console.log(")))))authoritylever", authoritylever);
    if (authoritylever !== 2) {
        assert(false, 422, "您没有权限删除用户!");
    }
    console.log(req.params.id);
    const model = await AdminUser.findByIdAndDelete(req.params.id);
    res.send(model);
}


//用户详细
exports.adminMessage = async (req, res) => {
    // console.log(req._id);
    // console.log(req.params.id);
    // console.log("$$$$", req.params.id  || 5225);
    // let model = await AdminMessage.findOne({userId:( req.params._id ==="undefind" ? req._id : req.params._id)});
    if (req.params.id !== "undefined") {
        console.log(req.params.id)
        model = await AdminMessage.findOne({userId: req.params.id});
    } else {
        model = await AdminMessage.findOne({userId: req._id});
    }
    // console.log("model", model);
    // console.log(model.name);
    res.send(model);
}

//编辑用户详细
exports.messageEdit = async (req, res) => {
    // console.log(req.body);
    const userMessage = await AdminMessage.findOne({userId: req._id});
    // console.log(userMessage.authoritylever);
    // assert(userMessage.authoritylever === 2,422,"您没有管理员权限!")
    res.send("ok");
    const model = await AdminMessage.findByIdAndUpdate(req.body._id, req.body);
    // console.log("@@@@", model);
}

//图片上传
exports.uploadImages = async (req, res) => {
    console.log(123)
    // console.log(req.file);
    req.file.url = `http://localhost:3000/uploads/${req.file.filename}`
    res.send({
        code: 200,
        message: {
            test: "图片上传",
            file: req.file
        }
    });
}

//登录
exports.loginHander = async (req, res, next) => {
    // console.log(req.body);
    // res.send(req.body);
    const {password} = req.body
    const user = await AdminUser.findOne({name: req.body.name}).select("+password")
    console.log(user);
    // assert(user, 422, "草泥马");
    try {
        assert(user, 422, "草泥马")
    } catch (err) {
        next(err)
    }

    const isValid = await require("bcrypt").compareSync(password, user.password);
    // assert(isValid, 422, "密码草泥马的错误");
    try {
        assert(isValid, 422, "密码草泥马的错误")
    } catch (err) {
        next(err)
    }

    // console.log(req.body);
    const jwt = require("jsonwebtoken")
    const token = jwt.sign({_id: user._id}, req.app.get("secret"));
    res.send({token})
}